/*
 * @Author: Theoton
 * @Date: 2018-03-27 10:11:20
 * @Last Modified by: Theoton
 * @Last Modified time: 2018-07-04 15:59:27
 */
import { observable, action } from 'mobx';
import { message } from 'antd';

import Api from '../../apis/new';
import { WeaLocaleProvider } from 'ecCom';

const getLabel = WeaLocaleProvider.getLabel;
const initState = {
  visible: false,
  loading: false,
  groupName: '',
  list: [],
  updateMailAddress: '0', // 更新联系人昵称
};

export default class EmailCreateGroupStore {
  @observable visible = initState.visible;
  @observable loading = initState.loading;
  @observable groupName = initState.groupName;
  @observable list = initState.list;
  @observable updateMailAddress = initState.updateMailAddress;


  @action
  setState = (params = {}) => {
    Object.keys(params).forEach((key) => {
      this[key] = params[key];
    });
    return this;
  }

  @action
  clear = () => this.setState(initState)

  @action
  addContactGroup = async (params) => {
    this.loading = true;
    const res = await Api.addContactGroup(params);
    try {
      if (res.api_status === true) {
        message.success(getLabel(522519, '添加联系人组成功'));
      } else {
        message.error(res.msg);
      }
    } catch (e) {
      message.error(e);
    }
    this.loading = false;
    return res;
  }

  @action
  addInnerGroup = async (params) => {
    this.loading = true;
    const res = await Api.addInnerGroup(params);
    try {
      if (res.api_status === true) {
        message.success(getLabel(384377, '添加常用组成功'));
      } else {
        message.error(res.msg);
      }
    } catch (e) {
      message.error(e);
    }
    this.loading = false;
    return res;
  }
}
